<?php

namespace Lib\PayApi\Zito;
use Lib\Utils\Utils;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/22
 * Time: 1:36
 */
class PaymentClient {
    public static function pay($orderNo, $money, $pbank) {
        $zito = C('ZITO');
        $banks = explode('_',$pbank);
        $requestarray = array(
            "id" => $zito['merchantId'],
            "appid" => $zito['appid'],
            "orderidinf" => $orderNo,
            "totalPrice" => sprintf("%.2f", $money),
            "key" => $zito['key'],
        );

        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str . $val ;
        }
        $sign = md5($md5str);

        $requestarray["posttime"] = self::udate("YmdHiZ");//date("YmdHis");
        $requestarray["ordertitle"] = $zito['ordertitle'];
        $requestarray["goodsname"] = $zito['goodsname'];
        $requestarray["goodsdetail"] = $zito['goodsdetail'];
        $requestarray["gid"] = $zito['tongdao'][$banks[0]][$banks[1]];
        $requestarray["bgRetUrl"] = $zito['notifyUrl'];
        $requestarray["returnUrl"] = $zito['returnUrl'];
        // $requestarray["openid"] = $zito['oxc7JwALuOiBNLZRbdS4rPlMmO7Y'];
        $requestarray["sign"] = $sign;

        // var_dump($requestarray);
        return Utils::post($zito['baseUri'], $requestarray);
    }

    private static function udate($format = 'u', $utimestamp = null) {
        if (is_null($utimestamp))
            $utimestamp = microtime(true);

        $timestamp = floor($utimestamp);
        $milliseconds = round(($utimestamp - $timestamp) * 1000000);

        return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
    }

    public static function query($orderNo) {
        $zito = C('ZITO');
        $requestarray = array(
            "id" => $zito['merchantId'],
            "appid" => $zito['appid'],
            "key" => $zito['key'],
        );

        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str . $val ;
        }
        $sign = md5($md5str);

        $requestarray["posttime"] = self::udate("YmdHiZ");
        $requestarray["orderId"] = $orderNo;//date("YmdHis");
        $requestarray["sign"] = $sign;
        return Utils::post($zito['findUri'], $requestarray);
    }
}